﻿@{
    ViewData["Title"] = "新建表";
}
<div class="layui-layout layui-layout-admin">
    <table class="layui-hide" id="lsttable"></table>
    <script type="text/html" id="tplEffectType">
        <button class="layui-btn layui-btn-primary dropdpwn-effectType">
            <span>{{= (app.effectType.filter(item => item.id == d.effectType)[0]).title }}</span>
            <i class="layui-icon layui-icon-down layui-font-12"></i>
        </button>
    </script>
    <script type="text/html" id="tplQueryType">
        <button class="layui-btn layui-btn-primary dropdpwn-queryType">
            <span>{{= (app.queryType.filter(item => item.id == d.queryType)[0]).title }}</span>         
            <i class="layui-icon layui-icon-down layui-font-12"></i>
        </button>
    </script>
    <script type="text/html" id="tplDictTypeCode">
         <button class="layui-btn layui-btn-primary dropdpwn-dictTypeCode">
            <span>
                {{#  var d = app.dictType.filter(item => item.id == d.dictTypeCode) }}
                {{= d.length>0?d[0].title:'请选择' }}
            </span>
            <i class="layui-icon layui-icon-down layui-font-12"></i>
        </button>
    </script>
    <script type="text/html" id="tplYesNo">
        {{# var yes = eval('d.' + d.LAY_COL.field) == 'Y'?'checked':'' }}
        <input lay-filter="demo-checkbox-filter" type="checkbox" id="{{d.LAY_COL.field}}" {{yes}}>
    </script>
</div>

@section scripts {
    <script type="text/javascript">
        var app = { 
            effectType:[],
            queryType: [],
            dictType: [{ id: '', title: '请选择' }]
        };
        layui.use(['http', 'toast'], function () {
            var $ = layui.$, form = layui.form, table = layui.table, dropdown = layui.dropdown,
                http = layui.http, toast = layui.toast;
            //
            app.init = function () {
                let data = app.data || {};
                //控件类型
                var p1 = http.get('/api/sysDictData/dataList/code_gen_effect_type');
                //查询方式
                var p2 = http.get('/api/sysDictData/dataList/code_gen_query_type');
                //字典类型
                var p3 = http.get('/api/sysDictType/list');
                Promise.all([p1, p2, p3]).then((res) => {
                    console.log(res)
                            
                    $.each(res[0].result, function (i, v) {
                        app.effectType.push({ title: v.value, id: v.code });
                    })              
                    $.each(res[1].result, function (i, v) {
                        app.queryType.push({ title: v.value, id: v.code });
                    })         
                    $.each(res[2].result, function (i, v) {
                        app.dictType.push({ title: v.name, id: v.code });
                    });

                    //
                    http.get('/api/sysCodeGenConfig/list?CodeGenId=' + data.id).then((res) => {
                        table.render({
                            elem: '#lsttable',
                            cols: [[ //标题栏
                                { field: 'LAY_NUM', title: '序号', width: 40 },
                                { field: 'propertyName', title: '实体属性', width: 120 },
                                { field: 'columnComment', title: '描述', edit: 'text' },
                                { field: 'netType', title: '数据类型', width: 100 },
                                { field: 'effectType', title: '控件类型', width: 160, templet: '#tplEffectType' },
                                { field: 'dictTypeCode', title: '字典', width: 200, templet: '#tplDictTypeCode' },
                                { field: 'whetherTable', title: '列表显示', width: 90, templet: '#tplYesNo' },
                                { field: 'whetherAddUpdate', title: '增改', align: 'center', width: 150, templet: '#tplYesNo' },
                                { field: 'whetherRequired', title: '必填', align: 'center', width: 80, templet: '#tplYesNo' },
                                { field: 'whetherSortable', title: '可排序', align: 'center', width: 80, templet: '#tplYesNo' },
                                { field: 'queryWhether', title: '是否是查询', align: 'center', width: 100, templet: '#tplYesNo' },
                                { field: 'queryType', title: '查询方式', width: 150, templet: '#tplQueryType' },
                                { field: 'orderNo', title: '排序', width: 60, edit: 'text' },
                            ]],
                            css: [
                                '.layui-table-cell{height: 50px; line-height: 40px;}'
                            ].join(''),
                            data: res.result,
                            done: function (res, curr, count) {
                                var options = this;
                                table.getRowData = function (tableId, elem) {
                                    var index = $(elem).closest('tr').data('index');
                                    return table.cache[tableId][index] || {};
                                };
                                form.on('checkbox(demo-checkbox-filter)', function (data) {
                                    var elem = data.elem;
                                    var data = table.getRowData(options.id, elem);
                                    data[elem.id] = elem.checked ? 'Y' : 'N';
                                });

                                dropdown.render({
                                    elem: '.dropdpwn-effectType',
                                    data: app.effectType,
                                    click: function (obj) {
                                        var data = table.getRowData(options.id, this.elem);
                                        this.elem.find('span').html(obj.title);
                                        data.effectType = obj.id;
                                    }
                                });
                                dropdown.render({
                                    elem: '.dropdpwn-queryType',
                                    data: app.queryType,
                                    click: function (obj) {
                                        var data = table.getRowData(options.id, this.elem);
                                        this.elem.find('span').html(obj.title);
                                        data.queryType = obj.id;
                                    }
                                });
                                dropdown.render({
                                    elem: '.dropdpwn-dictTypeCode',
                                    data: app.dictType,
                                    click: function (obj) {
                                        var data = table.getRowData(options.id, this.elem);
                                        this.elem.find('span').html(obj.title);
                                        data.dictTypeCode = obj.id;
                                    }
                                });
                            }
                        });
                    })

                })               
            }();
            app.save = function () {
                var postData = table.getData('lsttable');
                http.post('/api/sysCodeGenConfig/update', postData).then((res) => {
                    if (res.code == 200) {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        toast.success({ message: '保存成功！' });
                    }
                    else {
                        layer.alert('保存失败，错误信息:' + res.message, { icon: 2 });
                    }
                });

                return false;
            }
        })
    </script>
}
